' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' Copyright (c) 2006 Microsoft Corporation. All rights reserved.

' Original code from the Microsoft Windows SDK for Windows Vista.

Imports System.Collections.Generic
Imports Microsoft.Feeds.Interop

Friend Class CommonFeedListUtils

    ' Enumerates over all feeds available in all folders.
    Public Shared Function CommonFeedList(ByVal folder As IFeedFolder) As IEnumerable(Of IFeed)
        Dim queue As Queue(Of IFeedFolder) = New Queue(Of IFeedFolder)()
        Dim allFeeds As New List(Of IFeed)
        queue.Enqueue(folder)
        Do While queue.Count > 0
            Dim currentFolder As IFeedFolder = queue.Dequeue()
            For Each subfolder As IFeedFolder In CType(currentFolder.Subfolders, IFeedsEnum)
                queue.Enqueue(subfolder)
            Next subfolder

            For Each feed As IFeed In CType(currentFolder.Feeds, IFeedsEnum)
                allFeeds.Add(feed)
            Next
        Loop
        Return allFeeds
    End Function

    ' Enumerates only the feeds that have changed since the provided last write time.
    Public Shared Function LastWriteSince(ByVal folder As IFeedFolder, ByVal lastWriteTime As System.DateTime) As IEnumerable(Of IFeed)
        Dim allFeeds As New List(Of IFeed)
        For Each feed As IFeed In CommonFeedList(folder)

            If feed.LastWriteTime.ToLocalTime() > lastWriteTime Then
                allFeeds.Add(feed)
            End If
        Next feed
        Return allFeeds
    End Function
End Class